package com.example.demo.config.redis;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.locks.Lock;

/**
 * 创建锁工厂
 * @Description
 * @Author LLZ
 * @Date 2024/5/31
 */
@Component
public class DistributedLockFactory {

  @Resource
  private StringRedisTemplate stringRedisTemplate;

  private String lockName;

  private String uuid;

  public DistributedLockFactory() {
    this.uuid = IdUtil.simpleUUID();
  }


  /**
   * 获取分布式锁
   * @param lockType
   * @param lockName
   * @param expire
   * @return
   */
  public Lock getDistributedLock(String lockType, String lockName, long expire) {

    if (StrUtil.isEmpty(lockType)) {
      return null;
    }

    if ("REDIS".equalsIgnoreCase(lockType)) {
      return new DistributedRedisLock(stringRedisTemplate, lockName, uuid, expire);
    }

    if ("REDISSON".equalsIgnoreCase(lockType)) {
      return null;
    }

    if ("REDIS_DISTRIBUTED".equalsIgnoreCase(lockType)) {
      return null;
    }

    if ("ZOOKEEPER".equalsIgnoreCase(lockType)) {
      return null;
    }

    return null;
  }
}
